#include "blockqueue_test.hpp"
#include "task.hpp"

#include <unistd.h>
#include <ctime>

void* Comsumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);

    while(1)
    {
        Task t = bq->Pop();
        t();
        std::cout << "处理任务：" << t.GetTask() << " 结果：" << t.GetResult() << std::endl;
    }
}

void* Productor(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);

    while(1)
    {
        sleep(1);
        int data1 = rand() % 10 + 1;
        int data2 = rand() % 10;
        char op = opers[rand() % opers.size()];
        Task t(data1, data2, op);
        bq->Push(t);
        std::cout << "推送任务：" << t.GetTask() << std::endl;
    }
}

int main()
{
    srand(time(nullptr));

    BlockQueue<Task>* bq = new BlockQueue<Task>();

    pthread_t c, p;
    pthread_create(&c, nullptr, Comsumer, (void*)bq);
    pthread_create(&p, nullptr, Productor, (void*)bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}